﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Weibo2Sfdc
{
    public class WeiboEnquiryEngine
    {
        public event EventHandler Done;
        public bool _isStart = false;
        public WeiboParse _weiboParse = new WeiboParse();
        public WebBrowser web_main;
        public Condition _condition;
        public SearchKeyword _searchKeyword = new SearchKeyword();
        private static WeiboEnquiryEngine _instance;

        

        public WeiboParse WeiboParse
        {
            get
            {
                return _weiboParse;
            }
            set
            {
                _weiboParse = value;
            }
        }

        public SearchKeyword SearchKeyword
        {
            get
            {
                return _searchKeyword;
            }
            set
            {
                _searchKeyword = value;
            }
        }

        public SyncEngine SyncEngine
        {
            get
            {
                return SyncEngine.GetInstance();
            }
        }

        public Condition Condition
        {
            get
            {
                return _condition;
            }
            set
            {
                _condition = value;
            }
        }

        public DataCenter DataCenter
        {
            get
            {
                return DataCenter.GetInstance();
            }
        }
    
        public static WeiboEnquiryEngine GetInstance()
        {
            if (_instance == null)
            {
                _instance = new WeiboEnquiryEngine();
            }
            return _instance;
        }

        public void Start()
        {
            Reset();
            string url = "http://s.weibo.com/weibo/" + SearchKeyword.Keyword[0];
            WeiboParse.web_main = web_main;
            web_main.Navigate(url);
            _isStart = true;
        }

        //读取多个关键字
        public void Start2(ArrayList listKeywords)
        {
            Reset();
            for (int i = 0; i < listKeywords.Count; i++)
            {
                _isStart = false;
                Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c kwc = (Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c)listKeywords[i];
                string url = "http://s.weibo.com/weibo/" + kwc.Name;
                WeiboParse.web_main = web_main;
                web_main.Tag = "1"; //正在执行本次操作 
                WeiboParse.kwc = kwc; //保存关联的Keyword

                web_main.Navigate(url);
                _isStart = true;                 
                
            }
            
        }


        //------------------------------

        //读取多个关键字
        public void Start3(ArrayList listKeywords)
        {
            Reset();
            for (int i = 0; i < listKeywords.Count; i++)
            {
                Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c kwc = (Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c)listKeywords[i];

                //实例化ThreadWithState类，为线程提供参数
                ThreadWithState tws = new ThreadWithState(kwc, web_main, this, WeiboParse);

                // 创建执行任务的线程，并执行
                Thread t = new Thread(new ThreadStart(tws.getKeyword3));
                t.Start(); 
               
            }

        }




        class ThreadWithState
        {
            private Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c kwc;
            private WebBrowser web_main;
            private WeiboEnquiryEngine wbEngine;
            private WeiboParse weiboparse;
            public ThreadWithState(Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c kwc1, WebBrowser web_main1 ,WeiboEnquiryEngine wbEngine1,WeiboParse weiboparse1)
            {
                kwc = kwc1;
                web_main = web_main1;
                wbEngine = wbEngine1;
                weiboparse = weiboparse1;
            }

            //根据关键字获取微博内容
            public void getKeyword3()
            {
                while (web_main.Tag.ToString()== "stop")
                {
                    Thread.Sleep(1000);
                }
                web_main.Tag = "stop"; //正在执行本次操作 

                wbEngine._isStart = false;
                //Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c kwc = (Weibo2Sfdc.SfWeb.TRIXPRO_WB__Keyword__c)listKeywords[i];
                string url = "http://s.weibo.com/weibo/" + kwc.Name;
                weiboparse.web_main = web_main;
                WeiboParse.kwc = kwc; //保存关联的Keyword 
                web_main.Navigate(url);
                wbEngine._isStart = true;

            }

        }

        //-----------------------------


        public void Reset()
        {
            _isStart = false;
            DataCenter.Beans.Clear();
        }

        public void Pause()
        {
            _isStart = false;
        }

        public void Stop()
        {
            _isStart = false;

            this.web_main.Tag = "0"; //本次执行结束
        }

        public void Continue()
        {
            excute();
        }

        private bool excute()
        {
            if (Condition.IsOK())
            {
                WeiboParse.StartParse();
                DataCenter.Beans.AddRange(WeiboParse.result);
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool doNext()
        {
            HtmlElementCollection linkTag = web_main.Document.GetElementsByTagName("a");
            foreach (HtmlElement he in linkTag)
            {
                //System.Console.WriteLine(he.InnerText);
                if (he.InnerText == "下一页")
                {
                    string link = he.GetAttribute("href");
                    web_main.Navigate(link);
                    return true;
                }
            }
            return false;
        }

        public void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!_isStart)
            {
                return;
            }
            //Thread.Sleep(1000);
            if (!excute())
            {
                Stop();
                Done.Invoke(this, new EventArgs());
                return;
            }
            
            if(!doNext())
            {
                Stop();
                Done.Invoke(this, new EventArgs());
            }
        }

    }
}
